function AddChatCommand(cmd, callback, prefixconst)
	table.insert(ChatCommands, { cmd = cmd, callback = callback, prefixconst = prefixconst })
end

--BANKER CHAT SYSTEM ABOVE:

function SaveMoneyInBank(ply,HowMuch)
	if not ply:Alive() then
		Notify(ply, 1, 4, "You're dead...")
		return ""
	end
	if HowMuch == nil then 
		Notify(ply, 1, 4, "You didn't specify how much you want.")
		return ""
	end
	if tonumber(HowMuch) == nil then
		Notify(ply, 1, 4, "You should specify a number.")
		return ""
	end
	if tonumber(HowMuch) <= 0 then
		Notify(ply, 1, 4, "You should specify a valid number")
		return ""
	end
	if not ply:CanAfford(tonumber(HowMuch)) then
		Notify(ply, 1, 4, "You can't afford that.")
		return ""
	end
	local trace = ply:GetEyeTraceNoCursor()
	local banker = nil
	if trace.Entity:IsValid() then
		if trace.Entity:IsPlayer() then
			if (trace.Entity:Team() == TEAM_banker and ply:GetPos():Distance(trace.Entity:GetPos()) < 150) then
				banker = trace.Entity
			end
		elseif trace.Entity:GetClass() == "bank_machine" then
			banker = trace.Entity
		end
	end
	if ply:Team() == TEAM_banker then
		banker = ply
	end
	if banker == nil then
		Notify(ply, 1, 4, "You need to aim a banker or bank machine and be close him.")
		return ""
	end
	if not banker.Bank and banker != ply then
		Notify(ply, 1, 4, "Bank closed")
		return ""
	end
	if banker.tax == nil then
		banker.tax = 0
	end
	ply:AddMoney(-HowMuch)
	if not banker:IsPlayer() and banker.Owner != ply then
		MoneyWithTaxes = HowMuch - (HowMuch * banker.tax / 100)
		banker.SavedMoney = HowMuch * banker.tax / 100
	elseif banker.Owner == ply then
		MoneyWithTaxes = HowMuch
	elseif banker != ply then
		MoneyWithTaxes = HowMuch - (HowMuch * banker.tax / 100)
		banker:AddMoney(HowMuch * banker.tax / 100)
		Notify(banker, 1, 4, "You won "..tostring(HowMuch * banker.tax / 100)..CUR.." because taxes")
	else
		MoneyWithTaxes = HowMuch
	end
	ply:AddMoneyToBank(MoneyWithTaxes)
	Notify(ply, 1, 4, "You have "..tostring(ply:GetMoneyOfBank()..CUR.." in bank now."))
	return ""
end
	
AddChatCommand("/savemoney", SaveMoneyInBank)
	
function GetMoneyFromBank(ply, HowMuch)
	if not ply:Alive() then
		Notify(ply, 1, 4, "You're dead...")
		return ""
	end
	if HowMuch == nil then
		Notify(ply, 1, 4, "You didn't specify how much you want.")
		return ""
	end
	if tonumber(HowMuch) == nil then
		Notify(ply, 1, 4, "You should specify a number.")
		return ""
	end
	if tonumber(HowMuch) <= 0 then
		Notify(ply, 1, 4, "You should specify a valid number")
		return ""
	end
	if tonumber(HowMuch) > ply:GetMoneyOfBank() then
		Notify(ply, 1, 4, "You don't have that money in the bank.")
		return ""
	end
	local trace = ply:GetEyeTraceNoCursor()
	local banker = nil
	if trace.Entity:IsValid() then
		if trace.Entity:IsPlayer() then
			if (trace.Entity:Team() == TEAM_banker and ply:GetPos():Distance(trace.Entity:GetPos()) < 150) then
				banker = trace.Entity
			end
		elseif trace.Entity:GetClass() == "bank_machine" then
			banker = trace.Entity
		end
	end
	if ply:Team() == TEAM_banker then
		banker = ply
	end
	if banker == nil then
		Notify(ply, 1, 4, "You need to aim a banker or bank machine and be close him.")
		return ""
	end
	if not banker.Bank and banker != ply then
		Notify(ply, 1, 4, "Bank closed")
		return ""
	end
	ply:AddMoney(HowMuch)
	ply:AddMoneyToBank(-HowMuch)
	Notify(ply, 1, 4, "You have "..tostring(ply:GetMoneyOfBank()..CUR.." in bank now."))
	return ""
end

AddChatCommand("/getmoney", GetMoneyFromBank)

function Changetax(ply, percent)
	if ply:Team() != TEAM_banker then		
		Notify(ply, 1, 4, "You're not a banker.")
		return ""
	end
	if percent == nil then 
		Notify(ply, 1, 4, "You didn't specify percent.")
		return ""
	end
	if tonumber(percent) == nil then 
		Notify(ply, 1, 4, "You should specify a number.")
		return ""
	end
	if tonumber(percent) >= 100 or tonumber(percent) < 0 then
		Notify(ply, 1, 4, "You can't specify that number.")
		return ""
	end
	ply.tax = percent
	if not ply.Bank then
		Notify(ply, 1, 4, "Changed taxes. Now taxes are "..percent.."%")
		return ""
	else
		RP_ActualDoSay(ply,"Changed taxes. Now in my bank taxes are "..percent.."%")
		return ""
	end
			
end

AddChatCommand("/tax", Changetax)

function CloseBank(ply)
	if ply:Team() != TEAM_banker then
		Notify(ply, 1, 4, "You're not a banker.")
		return ""
	end
	if not ply.Bank then
		Notify(ply, 1, 4, "Bank already closed")
		return ""
	end
	ply.Bank = false
	RP_ActualDoSay(ply,"Bank closed")
	return ""
end

AddChatCommand("/closebank", CloseBank)

function OpenBank(ply)
	if ply:Team() != TEAM_banker then
		Notify(ply, 1, 4, "You're not a banker.")
		return ""
	end
	if ply.Bank then
		Notify(ply, 1, 4, "Bank already opened")
		return ""
	end
	if ply.tax == nil then
		ply.tax = 0
	end
	ply.Bank = true
	RP_ActualDoSay(ply,"Bank opened with taxes at "..ply.tax.."%")
	return ""
end

AddChatCommand("/openbank", OpenBank)

function CheckMoney(ply)
	if not ply:Alive() then
		Notify(ply, 1, 4, "You're dead...")
		return ""
	end
	local trace = ply:GetEyeTraceNoCursor()
	local banker = nil
	if trace.Entity:IsValid() then
		if trace.Entity:IsPlayer() then
			if (trace.Entity:Team() == TEAM_banker and ply:GetPos():Distance(trace.Entity:GetPos()) < 150) then
				banker = trace.Entity
			end
		elseif trace.Entity:GetClass() == "bank_machine" then
			banker = trace.Entity
		end
	end
	if ply:Team() == TEAM_banker then
		banker = ply
	end
	if banker == nil then
		Notify(ply, 1, 4, "You need to aim a banker be close him.")
		return ""
	end
	if not banker.Bank and banker != ply then
		Notify(ply, 1, 4, "Bank closed")
		return ""
	end
	Notify(ply, 1, 4, "You have "..tostring(ply:GetMoneyOfBank()..CUR.." in bank"))
	return ""
end

AddChatCommand("/checkmoney",CheckMoney)

function CheckTax(ply)
	if not ply:Alive() then
		Notify(ply, 1, 4, "You're dead...")
		return ""
	end
	local banker = nil
	if ply:Team() == TEAM_banker then
		banker = ply
	end
	local trace = ply:GetEyeTraceNoCursor()
	if trace.Entity:IsValid() then
		if trace.Entity:IsPlayer() then
			if (trace.Entity:Team() == TEAM_banker and ply:GetPos():Distance(trace.Entity:GetPos()) < 150) then
				banker = trace.Entity
			end
		elseif trace.Entity:GetClass() == "bank_machine" then
			banker = trace.Entity
		end
	end
	if banker == nil then
		Notify(ply, 1, 4, "You need to aim a banker or bank machine and be close him.")
		return ""
	end
	if not banker.Bank and banker != ply then
		Notify(ply, 1, 4, "Bank closed")
		return ""
	end
	if banker == ply then
		Notify(ply, 1, 4, "You put the taxes to "..tostring(banker.tax).."%")
		return ""
	end
	if banker.tax == nil then
		banker.tax = 0
	end
	if !banker:IsPlayer() then
		Notify(ply, 1, 4, "The bank machine taxes are "..tostring(banker.tax).."%")
	else
		RP_ActualDoSay(banker,"The taxes in my bank are "..tostring(banker.tax).."%\n")
	end
	return ""
end

AddChatCommand("/checktax",CheckTax)

// CarDealer system above:

function BuyJeep(ply)
	if args == "" then return "" end
	
	local trace = { }
	trace.start = ply:EyePos()
	trace.endpos = trace.start + ply:GetAimVector() * 85
	trace.filter = ply

	if RPArrestedPlayers[ply:SteamID()] then return "" end
	local tr = util.TraceLine(trace)

	local cost = 1500
	
	if not ply:CanAfford(cost) then
		Notify(ply, 1, 4, "Can not afford this! ("..tostring(cost)..CUR..")")
		return ""
	end

	if ply:Team() == TEAM_CAR then
		ply:AddMoney(-cost)
		Notify(ply, 1, 3, "You bought a Jeep for $"..tostring(cost))
		local car = ents.Create("prop_vehicle_jeep_old")
		car:SetModel("models/buggy.mdl")
		car:SetKeyValue("vehiclescript","scripts/vehicles/jeep_test.txt")
		car:SetNWEntity("owning_ent", ply)
		car:SetNWString("Owner", "Shared")
		car:SetPos(tr.HitPos)
		car.nodupe = true
		car:Spawn()
		car.SID = ply.SID
		return ""
	else
		Notify(ply, 1, 3, "You must be a Car Dealer to buy this.")
	end
	return ""
end
AddChatCommand("/buyjeep", BuyJeep)

function PM(ply, args)
	Notify(ply, 1, 4, "PM disabled. Please, use /sms command.")
	return ("/pm "..args)
end

function SMS(ply, args)
	local namepos = string.find(args, " ")
	if not namepos then 
		Notify(ply, 1, 4, "You need to specify a name and message.")
		return "" 
	end

	local name = string.sub(args, 1, namepos - 1)
	local msg = string.sub(args, namepos + 1)
	local price = 10
	
	target = FindPlayer(name)

	if target == ply then
		Notify(ply, 1, 4, "You can't send messages to yourself.")
		return ""
	end
	if not target then
		Notify(ply, 1, 4, "Could not find player: " .. name)
		return ""
	end
	if not ply:CanAfford(price) then
		Notify(ply, 1, 4, "You can't afford "..tostring(price)..CUR)
		return ""
	end
	
	ply:AddMoney(- price)
	local col = team.GetColor(ply:Team())
	TalkToPerson(target, col, "(SMS) "..ply:Nick(),Color(255,255,255,255), msg, ply)
	TalkToPerson(ply, col, "(SMS) "..ply:Nick(), Color(255,255,255,255), msg, ply)
	return ""
end
AddChatCommand("/sms", SMS)

function DoVoteCop(ply, args)
	ply:ConCommand("/cp")
	return ""
end
AddChatCommand("/votecop", DoVoteCop)

function MakeCP(ply, args)
	local mayor = nil
	for k,v in pairs(player.GetAll()) do
		if v:Team() == TEAM_MAYOR then
			mayor = v
		end
	end
	if mayor == nil then
		ply:ChangeTeam(TEAM_POLICE)
		Notify(ply, 1, 4, "There's no mayor so you can be cop.")
		return ""
	end
	if mayor == ply then
		ply:ChangeTeam(TEAM_POLICE)
		Notify(ply, 1, 4, "Changed mayor for cop")
		return ""
	end
	if ply:HasPriv(CP) or ply:HasPriv(ADMIN) or ply:HasPriv(MAYOR) or ply:IsAdmin() or ply:Team() == TEAM_CHIEF then
		ply:ChangeTeam(TEAM_POLICE)
		Notify(ply, 1, 4, "Automatically changed to cop.")
	elseif ply.PromoToCop then
			ply:ChangeTeam(TEAM_POLICE)
			Notify(ply, 1, 4, "You accepted your promotion to cop.")
	else
		Notify(ply, 1, 4, "You need to be promoted by the mayor.")
	end
	ply.PromotedToCop = false
	return ""
end
AddChatCommand("/cp", MakeCP)
AddChatCommand("/cop", MakeCP)

function BuyBankMachine(ply)
	local trace = { }
	trace.start = ply:EyePos()
	trace.endpos = trace.start + ply:GetAimVector() * 85
	trace.filter = ply
	
	if RPArrestedPlayers[ply:SteamID()] then return "" end
	local tr = util.TraceLine(trace)
	
	if ! tr.HitWorld then
		Notify(ply, 1, 3, "You can't place bank machine there.")
		return ""
	end
	
	local cost = 600
	
	if not ply:CanAfford(cost) then
		Notify(ply, 1, 4, "Can not afford this! ("..tostring(cost)..CUR..")")
		return ""
	end

	if ply:Team() == TEAM_banker then
		ply:AddMoney(-cost)
		Notify(ply, 1, 3, "You bought a Banker Machine for "..CUR..tostring(cost))
		local bm = ents.Create("bank_machine")
		bm:SetPos(tr.HitPos + ply:GetAimVector() * 3.2)
		AngleInverted = Angle(ply:GetAngles().p - 180,ply:GetAngles().y, ply:GetAngles().r -180)
		bm:SetAngles(AngleInverted)
		bm:SpawnFunction(ply) 
		bm.nodupe = true
		bm:SetMoveType(MOVETYPE_NONE)
		bm.cost = cost
		return ""
	else
		Notify(ply, 1, 3, "You must be a Banker to buy this.")
	end
	return ""
end
AddChatCommand("/buybankermachine", BuyBankMachine)
AddChatCommand("/buybm", BuyBankMachine)

function PlaceBankMachine(ply)
	local trace = { }
	trace.start = ply:EyePos()
	trace.endpos = trace.start + ply:GetAimVector() * 85
	trace.filter = ply
	
	local tr = util.TraceLine(trace)

	if ! tr.HitWorld then
		Notify(ply, 1, 3, "You can't place bank machine there.")
		return ""
	end
	
	if ply.BmOnInventory != nil then
		local bm = ply.BmOnInventory
		bm:SetPos(tr.HitPos + ply:GetAimVector() * 3.2)
		AngleInverted = Angle(ply:GetAngles().p - 180,ply:GetAngles().y, ply:GetAngles().r -180)
		bm:SetAngles(AngleInverted)
		ply.BmOnInventory = nil
		return ""
	else
		Notify(ply, 1, 3, "You don't are moving bank machine")
	end
	return ""
end
AddChatCommand("/placebankermachine", PlaceBankMachine)
AddChatCommand("/placebm", PlaceBankMachine)

function GetBankMachine(ply)
	if ply.BmOnInventory != nil then
		Notify(ply, 1, 3, "You already are moving a bank machine")
		return ""
	end
	if ply:GetEyeTraceNoCursor().Entity:GetClass() != "bank_machine" then
		Notify(ply, 1, 3, "That is not a bank machine.")	
		return ""
	end
	if ply:GetEyeTraceNoCursor().Entity.Owner != ply then
		Notify(ply, 1, 3, "That is not your bank machine.")	
		return ""
	end
	ply.BmOnInventory = ply:GetEyeTraceNoCursor().Entity
	Notify(ply, 1, 3, "Moving bank machine. To place it, use /placebm")	
	return ""
end
AddChatCommand("/getbankermachine", GetBankMachine)
AddChatCommand("/getbm", GetBankMachine)

function SellBankMachine(ply)
	if ply:GetEyeTraceNoCursor().Entity:GetClass() != "bank_machine" then
		Notify(ply, 1, 3, "That is not a bank machine.")	
		return ""
	end
	if ply:GetEyeTraceNoCursor().Entity.Owner != ply then
		Notify(ply, 1, 3, "That is not your bank machine.")	
		return ""
	end
	local price = ply:GetEyeTraceNoCursor().Entity.cost
	ply:AddMoney(price * 0.8)
	ply:GetEyeTraceNoCursor().Entity:Remove()
	Notify(ply, 1, 3, "Bank machine sold for "..tostring(price * 0.8)..CUR)	
	return ""
end
AddChatCommand("/sellbankermachine", SellBankMachine)
AddChatCommand("/sellbm", SellBankMachine)

function Changetaxofbm(ply, percent)
	if percent == nil then 
		Notify(ply, 1, 4, "You didn't specify percent.")
		return ""
	end
	if tonumber(percent) == nil then 
		Notify(ply, 1, 4, "You should specify a number.")
		return ""
	end
	if ply:GetEyeTraceNoCursor().Entity:GetClass() != "bank_machine" then
		return ""
	end
	if ply:GetEyeTraceNoCursor().Entity.Owner != ply then
		return ""
	end
	if tonumber(percent) >= 100 or tonumber(percent) < 0 then
		Notify(ply, 1, 4, "You can't specify that number.")
		return ""
	end
	ply:GetEyeTraceNoCursor().Entity.tax = percent
	if not ply:GetEyeTraceNoCursor().Entity.Bank then
		Notify(ply, 1, 4, "Changed taxes. Now taxes are "..percent.."%")
		return ""
	else
		RP_ActualDoSay(ply,"Changed taxes. Now in my bank machine taxes are "..percent.."%")
		return ""
	end
			
end

AddChatCommand("/taxofbm", Changetaxofbm)

function CloseBankofbm(ply)
	if ply:GetEyeTraceNoCursor().Entity:GetClass() != "bank_machine" then
		return ""
	end
	if ply:GetEyeTraceNoCursor().Entity.Owner != ply then
		return ""
	end
	if not ply:GetEyeTraceNoCursor().Entity.Bank then
		Notify(ply, 1, 4, "Bank machine already closed")
		return ""
	end
	ply:GetEyeTraceNoCursor().Entity.Bank = false
	RP_ActualDoSay(ply,"Bank machine closed")
	return ""
end

AddChatCommand("/closebankofbm", CloseBankofbm)

function OpenBankofbm(ply)
	if ply:GetEyeTraceNoCursor().Entity:GetClass() != "bank_machine" then
		return ""
	end
	if ply:GetEyeTraceNoCursor().Entity.Owner != ply then
		return ""
	end
	if ply:GetEyeTraceNoCursor().Entity.Bank then
		Notify(ply, 1, 4, "Bank machine already opened")
		return ""
	end
	if ply:GetEyeTraceNoCursor().Entity.tax == nil then
		ply:GetEyeTraceNoCursor().Entity.tax = 0
	end
	ply:GetEyeTraceNoCursor().Entity.Bank = true
	RP_ActualDoSay(ply,"Bank machine opened with taxes at "..ply:GetEyeTraceNoCursor().Entity.tax.."%")
	return ""
end

AddChatCommand("/openbankofbm", OpenBankofbm)

function GetMoneyofbm(ply)
	if ply:GetEyeTraceNoCursor().Entity:GetClass() != "bank_machine" then
		return ""
	end
	if ply:GetEyeTraceNoCursor().Entity.Owner != ply then
		return ""
	end
	if !ply:GetEyeTraceNoCursor().Entity.SavedMoney then
		ply:GetEyeTraceNoCursor().Entity.SavedMoney = 0
	end
	if ply:GetEyeTraceNoCursor().Entity.SavedMoney == 0 then
		Notify(ply, 1, 4, "There is no money")
		return ""
	end
	if ply:GetEyeTraceNoCursor().Entity.tax == nil then
		ply:GetEyeTraceNoCursor().Entity.tax = 0
	end
	ply:AddMoney(ply:GetEyeTraceNoCursor().Entity.SavedMoney)
	ply:GetEyeTraceNoCursor().Entity.SavedMoney = 0
	return ""
end

AddChatCommand("/getmoneyofbm", GetMoneyofbm)